package cd.zhixuan.utils.recycler.helper;

import android.util.Log;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

/**
 * @author: HuZC
 * @Description:
 * @Date: 2022/1/7
 */
public class ItemDragHelper<T> extends ItemTouchHelper {


    public ItemDragHelper(final List<T> list, final OnMovedListener<T> onMovedListener) {
        this(list, 0, 0, onMovedListener);
    }

    /**
     * Creates an ItemTouchHelper that will work with the given Callback.
     * <p>
     * You can attach ItemTouchHelper to a RecyclerView via
     * {@link #attachToRecyclerView(RecyclerView)}. Upon attaching, it will add an item decoration,
     * an onItemTouchListener and a Child attach / detach listener to the RecyclerView.
     * <p>
     * callback The Callback which controls the behavior of this touch helper.
     *
     * @param headerCount 考虑有 HEADER 的情况
     * @param headerCount 考虑有 FOOTER 的情况
     */
    public ItemDragHelper(@Nullable final List<T> list, final int headerCount, final int footerCount, final OnMovedListener<T> onMovedListener) {
        super(new Callback() {
            private int startPosition = -1;
            private int endPosition = -1;
            private int mActionState = ACTION_STATE_IDLE;
            private RecyclerView mRecyclerView;

            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                if (list == null || list.size() == 0) {
                    return 0;
                }
                if (headerCount > 0 || footerCount > 0) {
                    int adapterPosition = viewHolder.getAdapterPosition() - headerCount;
                    if (adapterPosition < 0 || adapterPosition >= list.size()) {
                        return 0;
                    }
                }
                //首先回调的方法 返回int表示是否监听该方向
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                int swipeFlags = 0;//侧滑删除
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                super.onSelectedChanged(viewHolder, actionState);
                if (list == null || list.size() == 0) {
                    return;
                }
                int fromPos = startPosition;
                int toPos = endPosition;
                int listSize = list.size();
                if (viewHolder != null && actionState != ACTION_STATE_IDLE) {
                    fromPos = viewHolder.getAdapterPosition() - headerCount;
                    startPosition = fromPos;
                    if (onMovedListener != null && fromPos >= 0 && fromPos < listSize) {
                        onMovedListener.onStart(list, fromPos);
                    }
                } else {
                    if (onMovedListener != null && toPos >= 0 && toPos < listSize) {
                        onMovedListener.onEnd(list, toPos);
                    }
                }
                if (actionState == ACTION_STATE_DRAG) {
                    //开始拖拽
                    Log.e("ItemDragHelper", "开始拖拽  , " + "viewHolder==null is " + (viewHolder == null) + actionState);
                } else if (actionState == ACTION_STATE_SWIPE) {
                    //开始侧滑
                    Log.e("ItemDragHelper", "开始侧滑  , " + "viewHolder==null is " + (viewHolder == null) + actionState);
                } else if (actionState == ACTION_STATE_IDLE && mActionState == ACTION_STATE_DRAG) {
                    //停止拖拽
                    //修正position
                    if (fromPos >= 0 && fromPos < listSize && toPos >= 0 && toPos < listSize) {
                        if (mRecyclerView != null) {
                            RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
                            if (adapter != null) {
                                adapter.notifyItemRangeChanged(Math.min(fromPos + headerCount, toPos + headerCount),
                                        Math.abs(fromPos - toPos) + 1);
                            }
                        }
                    }
                    Log.e("ItemDragHelper", "停止拖拽  , " + "viewHolder==null is " + (viewHolder == null) + actionState);
                } else if (actionState == ACTION_STATE_IDLE && mActionState == ACTION_STATE_SWIPE) {
                    //停止侧滑
                    Log.e("ItemDragHelper", "停止侧滑  , " + "viewHolder==null is " + (viewHolder == null) + actionState);

                }

                mActionState = actionState;
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                if (list == null || list.size() == 0) {
                    return false;
                }
                mRecyclerView = recyclerView;
                if (recyclerView == null) return false;
                RecyclerView.Adapter adapter = recyclerView.getAdapter();
                if (adapter == null) return false;
                //滑动事件
                int to = target.getAdapterPosition() - headerCount;
                if (to < 0 || to >= list.size()) {
                    return false;
                }
                int from = viewHolder.getAdapterPosition() - headerCount;
                if (from < 0 || from >= list.size()) {
                    return false;
                }
                endPosition = to;
                Collections.swap(list, from, to);
                adapter.notifyItemMoved(from + headerCount, to + headerCount);
                //其他操作
                if (onMovedListener != null) {
                    onMovedListener.onMoved(list, from, to);
                }
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            }

            @Override
            public boolean isLongPressDragEnabled() {
                //是否可拖拽
                return true;
            }

        });
    }

    public interface OnMovedListener<M> {
        void onStart(List<M> list, int startPos);

        void onMoved(List<M> list, int posFrom, int posTo);

        void onEnd(List<M> list, int endPos);

    }

}
